package com.yanqu.road.server.cmd.activity.chuhan.cross;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.seasontask.SeasonTaskConditionData;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.ChuHanProto;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.Command;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.server.manger.activity.tongbing.ChuHanMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;
import io.netty.channel.ChannelHandlerContext;

import java.math.BigInteger;


@Cmd(code = GameProtocol.S_CROSS_CHUHAN_BARRACKS_OP, desc = "")
public class ChuHanBarracksOpCmd implements Command {

    @Override
    public void execute(ChannelHandlerContext ctx, YanQuMessage packet) throws Exception {
        ChuHanProto.CrossChuHanBarracksOpMsg reqMsg = ChuHanProto.CrossChuHanBarracksOpMsg.parseFrom(packet.getBytes());
        long userId = packet.getPlayerId();
        GamePlayer player = GamePlayerMgr.getOnlinePlayer(userId);
        if (player != null) {

            if (reqMsg.getType() == 1) {
                if (!StringUtils.isNullOrEmpty(reqMsg.getCost())) {
                    player.getModule(PreReduceModule.class).restorePreReduce(reqMsg.getCost());
                    Property cost = PropertyHelper.parseStringToProperty(reqMsg.getCost());
                    player.getModule(CurrencyModule.class).removeCurrency(cost,
                            eLogMoneyType.ChuHan, eLogMoneyType.ChuHanBarracksCallSoldier);
                    // 消耗兵符则发事件
                    BigInteger costBingFuCount = cost.getCountByGoodsId(ChuHanMgr.getConfig().getBingFuItem());
                    ActivityInfo actInfo = ChuHanMgr.getActivityInfo();
                    if (null != actInfo && costBingFuCount.intValue() > 0){
                        SeasonTaskConditionData data = new SeasonTaskConditionData(BigInteger.valueOf((long)actInfo.getType()), costBingFuCount);
                        player.notifyListener(eGamePlayerEventType.TongbinCostStaminaTask.getValue(), data);
                    }
                }
            } else if (reqMsg.getType() == 2) {
                if (!StringUtils.isNullOrEmpty(reqMsg.getCost())) {
                    player.getModule(PreReduceModule.class).restorePreReduce(reqMsg.getCost());
                }
            } else if (reqMsg.getType() == 3) {
                // 无事发生
            } else if (reqMsg.getType() == 4) {
                if (!StringUtils.isNullOrEmpty(reqMsg.getCost())) {
                    player.getModule(CurrencyModule.class).addCurrency(reqMsg.getCost(), eLogMoneyType.ChuHan, eLogMoneyType.ChuHanBarracksCancel);
                }
            }
            player.sendPacket(Protocol.S_CHUHAN_RECRUIT_SOLDIER % 20000, reqMsg.getResMsg().toBuilder());
        }
    }

}
